<template>
	<view class="content">
		<view class="con-head">AI对话记录</view>
		<!-- 		<scroll-view scroll-x="true" style=" white-space:nowrap; width: 100%;">
			<view style="width:100%;display: flex">
				<view v-for="(item, index) in roleList" :key="index" class="con-tab">
					<view :class="tabIndex==index ? 'tabBoxActive' : 'tabBox'" @click="selectRole(index)" style="">
						<text>{{item.name}}</text>
					</view>
				</view>
			</view>
		</scroll-view> -->
		<view style="padding:0 5px 0 10px;text-align: center;" @click="openRoleDialog">
			{{roleList[tabIndex].name}}<uni-icons type="down" size="" color="#fff"></uni-icons>
		</view>
		<view v-if="list.length>0" style="padding: 10px 5px 30px 5px;">
			<scroll-view class="scroll-view" :style="{height:scrollViewHeight+'px'}" :scroll-y="true"
				:scroll-top="scrollTop" :scroll-with-animation="true">
				<view id="scroll-view-content">
					<view class="con-mid" v-for="(item,index) in  list" :key="index">
						<view class="mid-right" v-if="item.role=='user'">
							<view class="right-text">{{item.content[0].text}}</view>
							<view class="right-img">
								<image src="../../static/logo.png" style="width: 50rpx;height: 50rpx;"
									mode="aspectFill">
								</image>
							</view>
						</view>
						<view class="mid-left" v-if="item.role=='assitant'">
							<view class="left-text">{{item.content[0].text}}</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view v-else class="empty">
			------------------------------
			暂无记录</view>
		<uni-popup ref="rolePopup" type="center" border-radius="10px" background-color="RGB(108, 104, 104, 0.69)">
			<view style="padding: 10px 10px 0;width: 150px;text-align: center;height: 220px;overflow: auto;">
				<view style="padding: 0 0 5px 0;color:#c5c5c5;">助理选择</view>
				<view v-for="(item,index) in roleList" :key="index" @click="selectRole(index)"
					style="padding:8px 0;border-top:0.5px solid #818181">{{item.name}}</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				scrollTop: 0,
				scrollViewHeight: 0,
				roleList: [{
						name: "我的助理",
						roleId: "DYDYGNjEjf3P",
						token: "g3zN7vYQz7UczV5HldF9a0TmPPqpciUf"
					},
					{
						name: "故事大王",
						roleId: "qaN9pxjLvSNy",
						token: "qlnNil8ngXukLqR7uRavSzpjK69RyZIi"
					},
					{
						name: "诗词大师",
						roleId: "q8OYD2HSpHNy",
						token: "hqTDbdoMK0qwEpp2CMPk5Otw7e4IRDXF"
					},
					{
						name: "英语训练",
						roleId: "VeQkwQ6llFxG",
						token: "RVJaZYapwAvOZCkogY3f3Fe6UDp23NER"
					},
					{
						name: "数学天才",
						roleId: "svlhxJKD0jHJ",
						token: "z2i3TzocjIqzKxyxeMANeJAQ9vk4qbav"
					},
					{
						name: "粤语达人",
						roleId: "QOtnQFhXzit3",
						token: "xQvF7U4lnUyWzUiORt3eB2a0LMk1LaAE"
					},
					{
						name: "百科问答",
						roleId: "eJOspNDvfjpv",
						token: "jddWJXpzQn2k1m7E9sUPu4ZkZD58qPi7"
					}, {
						name: "成语接龙",
						roleId: "d4E5dWuOXRfW",
						token: "dxfYNOuGiTif5Mx6lpiyUBJoRJ0H4cKb"
					},
					{
						name: "辩论家",
						roleId: "v2o6Gmy4ISck",
						token: "hB20Ke7mIS2UvAwEGvqvXBLu21Nl7LAH"
					},{
						name: "孔子",
						roleId: "pPG10z4SzI7y",
						token: "uVHKT5fuEVaUJ0euxgLG6qx0sur47geB"
					}

				],
				tabIndex: 0
			};
		},
		onShow() {
			this.selectRole(0)
			this.scrollToBottom();
		},
		created() {
			this.scrollViewHeight = uni.getSystemInfoSync().windowHeight - 100
		},
		methods: {
			scrollToBottom() {
				this.$nextTick(() => {
					uni.createSelectorQuery().in(this).select('#scroll-view-content').boundingClientRect((res) => {
						let top = res.height - this.scrollViewHeight + 37;
						if (top > 0) {
							this.scrollTop = top;
						}
					}).exec()
				})
			},
			selectRole(i) {
				this.tabIndex = i
				if (i == 0) {
					this.list = uni.getStorageSync('zhuli') || []
				} else if (i == 1) {
					this.list = uni.getStorageSync('gushi') || []
				} else if (i == 2) {
					this.list = uni.getStorageSync('shici') || []
				} else if (i == 3) {
					this.list = uni.getStorageSync('yingyu') || []
				} else if (i == 4) {
					this.list = uni.getStorageSync('shuxue') || []
				} else if (i == 5) {
					this.list = uni.getStorageSync('yueyu') || []
				} else if (i == 6) {
					this.list = uni.getStorageSync('baike') || []
				} else if (i == 7) {
					this.list = uni.getStorageSync('chengyu') || []
				} else if (i == 8) {
					this.list = uni.getStorageSync('bianlun') || []
				}  else if (i == 9) {
					this.list = uni.getStorageSync('kongzi') || []
				}else {
					this.list = []
				}
				this.$refs.rolePopup.close()
			},
			//打开助理选择弹窗
			openRoleDialog() {
				this.$refs.rolePopup.open('center')
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 70rpx 20rpx 10rpx 20rpx;

		.con-head {
			text-align: center;
		}

		.con-mid {
			margin: 30rpx 0 0 0;

			.mid-right {
				display: flex;
				justify-content: end;
				margin: 16rpx 0;
				align-items: center;


				.right-text {
					border: 1rpx solid #29ad9d;
					padding: 10rpx 20rpx;
					border-radius: 20rpx;
					max-width: 75%;
				}

				.right-img {
					border: 1rpx solid #29ad9d;
					border-radius: 50%;
					width: 50rpx;
					height: 50rpx;
					padding: 10rpx;
					margin-left: 10rpx;
					box-shadow: 0px 0px 8px #fff;
				}
			}

			.mid-left {


				.left-text {
					border: 1rpx solid RGB(48, 168, 168, 0.6);
					padding: 10rpx 20rpx;
					border-radius: 20rpx;

				}
			}
		}

		.con-tab {
			width: 70px;

			.tabBox {
				padding: 2px 5px;
			}

			.tabBoxActive {
				padding: 2px 5px;
				background-color: #413d3d;
				border-radius: 2px;
			}
		}


		.empty {
			text-align: center;
			margin-top: 40rpx;
			color: RGB(217, 217, 217, 0.5);
		}

	}
</style>